﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ADC.BL;
using ADC.Utilites;

namespace ADC.Web.Admin
{
    public partial class ManageProductType : System.Web.UI.Page
    {
        log4net.ILog logger = log4net.LogManager.GetLogger("ManageProductType");
        private int _mode;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitData();
            }
        }

        private void InitData()
        {
            // Check if the mode is existing
            if (Request.Params["mode"] != null)
            {

                try
                {
                    _mode = Convert.ToInt32(Request.Params["mode"].ToString());

                    if (_mode.Equals(1)) // Edit Mode
                    {
                        lblHeader.Text = "Cập nhật danh mục";
                        LinkButtonAddNew.Text = "Cập nhật";
                        if (Request.Params["ID"] != null)
                        {
                            var ID = Convert.ToInt32(Request.Params["ID"].ToString());
                            var productType = ProductTypeFactory.GetByID(ID);
                            txtTitle.Text = productType.Title;
                            if (productType.Sort != null && productType.Sort.Value != null)
                            {
                                txtOrder.Text = productType.Sort.Value.ToString();
                            } 
                            
                            txtDescription.Text = productType.Description;
                        }
                    }
                    else // New mode
                    {
                        lblHeader.Text = "Thêm mới danh mục";
                        LinkButtonAddNew.Text = "Thêm mới";
                    }

                }
                catch (Exception ex)
                {
                    logger.Error(ex.ToString());
                    if (GlobalInfo.IsEnableDebugMode) throw;
                }
            }

        }

        protected void LinkButtonAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                if (Request.Params["mode"] == null) return;
                _mode = Convert.ToInt32(Request.Params["mode"]);
                if (_mode.Equals(1)) // Edit 
                {
                    if (Request.Params["ID"] != null)
                    {
                        var ID = Convert.ToInt32(Request.Params["ID"].ToString());
                        var productType = new DAL.ProductType();
                        productType.ProductTypeID = ID;
                        productType.Title = txtTitle.Text;
                        productType.Description = txtDescription.Text;
                        productType.Sort = Convert.ToInt32(txtOrder.Text);

                        //Upload the file 

                        ProductTypeFactory.Update(productType);

                        Response.Redirect("ProductType.aspx", false);
                    }
                }
                else // New mode
                {
                    var productType = new DAL.ProductType();
                    productType.Title = txtTitle.Text;
                    productType.Description = txtDescription.Text;
                    productType.Sort = Convert.ToInt32(txtOrder.Text);
                    productType.ModifiedDate = DateTime.Now;
                    var result = ProductTypeFactory.Insert(productType);

                    if (result.ProductTypeID > 0)
                    {
                        Response.Redirect("ProductType.aspx", false);
                    }
                    else
                    {
                        lblErrorMessage.Visible = true;
                        lblErrorMessage.Text = Common.INSERT_FAIL;
                    }
                }

            }
            catch (Exception ex)
            {
                lblErrorMessage.Visible = true;
                lblErrorMessage.Text = Common.UPDATE_FAIL;
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }
    }
}

